<template>
	<view class="event_statistics">
		<picker mode="date" :value="date" :start="startDate" :end="endDate" @change="bindDateChange">
			<view style="text-align: center;">{{ date }}</view>
		</picker>
		<view class="event-chart">
			<text>事件类型占比图</text>
			<view class="charts-box">
				<qiun-data-charts type="pie" :opts="opts" :chartData="chartData" />
			</view>
		</view>
		<view v-if="eventList.length === 0" class="event-list">暂无数据</view>
		<view v-else class="event-list">
			<text>事件明细</text>
			<view class="event-item" v-for="(item, index) in eventList" :key="index">
				<view :style="{ width: '30rpx', height: '30rpx', background: item.eventColor }"></view>
				<view>{{ getEventTypeLabel(item) }}</view>
				<view style="margin-left: 20rpx;">
					<view>{{ item.eventTitle }}</view>
					<view>{{ item.eventStart }} - {{ item.eventEnd }}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>

export default {
	data() {
		return {
			date: this.getDate(),
			eventList: [],
			chartData: {},
			color:[],
			opts: {
				color: this.color,
				padding: [5, 5, 5, 5],
				enableScroll: false,
				extra: {
					pie: {
						activeOpacity: 0.5,
						activeRadius: 10,
						offsetAngle: 0,
						labelWidth: 15,
						border: true,
						borderWidth: 3,
						borderColor: "#FFFFFF",
						linearType: "none"
					}
				}
			}
		}
	},
	computed: {
		startDate() {
			return this.getDate('start');
		},
		endDate() {
			return this.getDate('end');
		}
	},
	onShow() {
		this.getEventList()
	},
	methods: {
		async getEventList() {
			let result = await this.$API.event.getEventList({ eventDate: this.date })
			if (result.code === 200) {
				let newList = result.data
				let newData = []
				let colorList = []
				if (newList.length === 0) {
					this.chartData = JSON.parse(JSON.stringify({
						series: [
							{
								data: [{ name: '暂无数据', value: 1}]
							}
						]
					}));
					this.opts.color = ['#cccccc']
					this.eventList = []
					return
				}
				newList.forEach(item => {
					item.eventStart = item.eventStart.slice(11, 16)
					item.eventEnd = item.eventEnd.slice(11, 16)
					if(item.eventType) {
						let index = newData.findIndex(data => data.name === item.eventType.eventTypeLabel)
						if (index === -1) {
							newData.push({
								name: item.eventType.eventTypeLabel,
								value: 1
							})
							colorList.push(item.eventType.eventTypeColor)
						} else {
							newData[index].value++
						}
					}
				})
				this.chartData = JSON.parse(JSON.stringify({
					series: [
						{
							data: newData
						}
					]
				}));
				this.opts.color = colorList
				if(newList.length > 1) {
					this.opts.extra.pie.border = true
				}else {
					this.opts.extra.pie.border = false
				}
				this.eventList = newList
			}
		},
		getEventTypeLabel(event) {
			if (event.eventType) {
				return event.eventType.eventTypeLabel
			} else {
				return ''
			}
		},
		getDate(type) {
			const date = new Date();
			let year = date.getFullYear();
			let month = date.getMonth() + 1;
			let day = date.getDate();

			if (type === 'start') {
				year = year - 60;
			} else if (type === 'end') {
				year = year + 2;
			}
			month = month > 9 ? month : '0' + month;
			day = day > 9 ? day : '0' + day;
			return `${year}-${month}-${day}`;
		},
		reset(){
			this.getEventList()
		},
		bindDateChange: function (e) {
			this.date = e.detail.value
			this.reset()
		},

	}
}
</script>

<style lang="scss" scoped>
.event_statistics {
	width: 100%;
	height: 100%;

	.event-chart {
		flex-direction: column;
		display: flex;
		justify-content:space-around;
		align-items: center;
		border-radius: 18rpx;
		border: 1rpx solid #E0E3DA;
		box-shadow: 2rpx 6rpx 0rpx #e5e8df;
		background-color: #ffffff;
		margin: 30rpx;
		padding: 25rpx;

		.charts-box {
			width: 100%;
			height: 500rpx;
		}
	}

	.event-list {
		display: flex;
		flex-direction: column;
		justify-content: start;
		margin: 30rpx;
		padding: 25rpx;
		border-radius: 18rpx;
		border: 1rpx solid #E0E3DA;
		box-shadow: 2rpx 6rpx 0rpx #e5e8df;
		background-color: #ffffff;

		>view {
			margin: 10rpx 0;
		}

		.event-item {
			display: flex;
			align-items: center;

			>view {
				margin: 0 10rpx;
			}
		}
	}
}
</style>
